// 与数据库打交道
const { create } = require('../../model/log')

const { Success, SysErr } = require('../../response/code')

class LogService {
  async create(ctx, data = null) {
    try {
      // data 为null时是通过 api请求的,不为null时为当前系统调用
      if (data === null) {
        data = ctx.request.body
        data.body = JSON.parse(data.body)
        data.owner_id = ctx.state.owner_id
        const result = await create(data)
        ctx.body = new Success(result).toString()
      } else {
        await create(data)
      }
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }
}
module.exports = new LogService()
